package FrontOffice;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import javax.swing.WindowConstants;



public class LoginOrRegister extends javax.swing.JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private JTextField Benutzername;
	private JPasswordField Passwort;
	private JTabbedPane jTabbedPaneRegister;
	private JTabbedPane jTabbedPaneLogin;
	private JButton RegisterButton;
	private JButton LogInButton;
	private JLabel Label_Passwort;
	private JLabel Label_Benutzername;
	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new LanguageSelect());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public LoginOrRegister() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(800, 600));
			this.setSize(800, 600);
			this.setLayout(null);
			{
				Benutzername = new JTextField();
				this.add(Benutzername);
				Benutzername.setBounds(384, 155, 197, 28);
			}
			{
				Passwort = new JPasswordField();
				this.add(Passwort);
				Passwort.setBounds(384, 217, 197, 28);
			}
			{
				Label_Benutzername = new JLabel();
				this.add(Label_Benutzername);
				Label_Benutzername.setText("Benutzername");
				Label_Benutzername.setBounds(178, 161, 167, 16);
			}
			{
				Label_Passwort = new JLabel();
				this.add(Label_Passwort);
				Label_Passwort.setText("Passwort");
				Label_Passwort.setBounds(178, 217, 167, 16);
			}
			{
				jTabbedPaneLogin = new JTabbedPane();
				this.add(jTabbedPaneLogin);
				jTabbedPaneLogin.setBounds(292, 282, 186, 95);
				{
					
					LogInButton = new JButton();
					jTabbedPaneLogin.addTab("Login", null, LogInButton, null);
					LogInButton.setBounds(334, 317, 109, 52);
					LogInButton.setText("auf geht's");
					LogInButton.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent evt) {
							LogInButtonActionPerformed(evt);
						}
					});
				}
			}
			{
				jTabbedPaneRegister = new JTabbedPane();
				this.add(jTabbedPaneRegister);
				jTabbedPaneRegister.setBounds(292, 377, 186, 93);
				{
					RegisterButton = new JButton();
					jTabbedPaneRegister.addTab("Neu hier?", null, RegisterButton, null);
					RegisterButton.setText("Registrieren");
					RegisterButton.setBounds(334, 403, 109, 52);
					RegisterButton.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent evt) {
							RegisterButtonActionPerformed(evt);
						}
					});
				}
			}
			this.setSize(800, 600);
			this.setAutoscrolls(true);
			Benutzername.addKeyListener(new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent arg0) {
					
				}
				
				@Override
				public void keyReleased(KeyEvent arg0) {
					
					if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
						LogInButtonActionPerformed(null);
					}
				}
				
				@Override
				public void keyPressed(KeyEvent arg0) {
									
				}
			});
			Passwort.addKeyListener((new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent arg0) {
					
					
				}
				
				@Override
				public void keyReleased(KeyEvent arg0) {
					
					if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
						LogInButtonActionPerformed(null);
					}
				}
				
				@Override
				public void keyPressed(KeyEvent arg0) {
					
				}
			}));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void RegisterButtonActionPerformed(ActionEvent evt) {
//		Start.getStart().getContentPane().removeAll();
//		RegisterNEW H = new RegisterNEW();
//		Start.getStart().add(H);
//		Start.getStart().validate();
		
		this.removeAll();
		add(new RegisterNEW());
		this.repaint();
	}

	@SuppressWarnings("deprecation")
	private void LogInButtonActionPerformed(ActionEvent evt) {
		
		try {
			if (!FrontOffice.Statements.login(Benutzername.getText(),
					Passwort.getText())) {
				// JFehler.setText("User existiert nicht. Versuchen Sie es erneut!");
				// ---> Textfeld fuer Fehlermeldung anlegen !!!!
			} else {
				// JFehler.setText("");
				FrontOffice.Statements.aktueler_nutzer = Benutzername.getText();
//				Start.getStart().getContentPane().removeAll();
//				FrontOffice.Hauptmenue HM= new FrontOffice.Hauptmenue();
//				Start.getStart().add(HM);
//				Start.getStart().validate();
				this.removeAll();
				add(new FrontOffice.Hauptmenue());
				this.repaint();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
}
